<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="快递上填的名字" prop="realName">
      <el-input v-model="dataForm.realName" placeholder="快递上填的名字"></el-input>
    </el-form-item>
    <el-form-item label="用户id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="快递上的手机号" prop="phone">
      <el-input v-model="dataForm.phone" placeholder="快递上的手机号"></el-input>
    </el-form-item>
    <el-form-item label="快递地址" prop="deliveryAddress">
      <el-input v-model="dataForm.deliveryAddress" placeholder="快递地址"></el-input>
    </el-form-item>
    <el-form-item label="订单id" prop="orderId">
      <el-input v-model="dataForm.orderId" placeholder="订单id"></el-input>
    </el-form-item>
    <el-form-item label="快递单号" prop="expressNum">
      <el-input v-model="dataForm.expressNum" placeholder="快递单号"></el-input>
    </el-form-item>
    <el-form-item label="逻辑删除位" prop="showStatus">
      <el-input v-model="dataForm.showStatus" placeholder="逻辑删除位"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="商品的id" prop="productId">
      <el-input v-model="dataForm.productId" placeholder="商品的id"></el-input>
    </el-form-item>
    <el-form-item label="商品名" prop="productName">
      <el-input v-model="dataForm.productName" placeholder="商品名"></el-input>
    </el-form-item>
    <el-form-item label="扣除的积分" prop="point">
      <el-input v-model="dataForm.point" placeholder="扣除的积分"></el-input>
    </el-form-item>
    <el-form-item label="送货地区" prop="area">
      <el-input v-model="dataForm.area" placeholder="送货地区"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          realName: '',
          userId: '',
          phone: '',
          deliveryAddress: '',
          orderId: '',
          expressNum: '',
          showStatus: '',
          createTime: '',
          updateTime: '',
          productId: '',
          productName: '',
          point: '',
          area: ''
        },
        dataRule: {
          realName: [
            { required: true, message: '快递上填的名字不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '快递上的手机号不能为空', trigger: 'blur' }
          ],
          deliveryAddress: [
            { required: true, message: '快递地址不能为空', trigger: 'blur' }
          ],
          orderId: [
            { required: true, message: '订单id不能为空', trigger: 'blur' }
          ],
          expressNum: [
            { required: true, message: '快递单号不能为空', trigger: 'blur' }
          ],
          showStatus: [
            { required: true, message: '逻辑删除位不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          productId: [
            { required: true, message: '商品的id不能为空', trigger: 'blur' }
          ],
          productName: [
            { required: true, message: '商品名不能为空', trigger: 'blur' }
          ],
          point: [
            { required: true, message: '扣除的积分不能为空', trigger: 'blur' }
          ],
          area: [
            { required: true, message: '送货地区不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/modules/mallorder/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.realName = data.mallOrder.realName
                this.dataForm.userId = data.mallOrder.userId
                this.dataForm.phone = data.mallOrder.phone
                this.dataForm.deliveryAddress = data.mallOrder.deliveryAddress
                this.dataForm.orderId = data.mallOrder.orderId
                this.dataForm.expressNum = data.mallOrder.expressNum
                this.dataForm.showStatus = data.mallOrder.showStatus
                this.dataForm.createTime = data.mallOrder.createTime
                this.dataForm.updateTime = data.mallOrder.updateTime
                this.dataForm.productId = data.mallOrder.productId
                this.dataForm.productName = data.mallOrder.productName
                this.dataForm.point = data.mallOrder.point
                this.dataForm.area = data.mallOrder.area
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/modules/mallorder/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'realName': this.dataForm.realName,
                'userId': this.dataForm.userId,
                'phone': this.dataForm.phone,
                'deliveryAddress': this.dataForm.deliveryAddress,
                'orderId': this.dataForm.orderId,
                'expressNum': this.dataForm.expressNum,
                'showStatus': this.dataForm.showStatus,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'productId': this.dataForm.productId,
                'productName': this.dataForm.productName,
                'point': this.dataForm.point,
                'area': this.dataForm.area
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
